package artist.graphic.cn.UI.Fragment;

import android.app.Fragment;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v7.app.NotificationCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.HashMap;
import artist.graphic.cn.R;
import artist.graphic.cn.UI.AboutUsActivity;
import artist.graphic.cn.UI.HTMLActivity;
import artist.graphic.cn.UI.LoginActivity;
import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import cn.smssdk.gui.RegisterPage;

import static android.content.Context.NOTIFICATION_SERVICE;

/**
 * Created by wanghuanlong on 2018/1/19.
 */

public class UserInMainFragment extends Fragment implements View.OnClickListener{

    boolean isNightMode=false;
    RelativeLayout el_hearing_enhancement,el_message,el_celve, el_training_camp, el_collect, el_discount, ll_calendar, el_about_us;
    CheckBox cb_night_mode;
    ImageView iv_user_icon;
    Button btn_login,btn_registered;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        View view=inflater.inflate(R.layout.frag_personal_accounts,container,false);
        el_hearing_enhancement = (RelativeLayout) view.findViewById(R.id.el_hearing_enhancement);
        el_hearing_enhancement.setOnClickListener(this);
        el_message = (RelativeLayout) view.findViewById(R.id.el_message);
        el_message.setOnClickListener(this);
        el_celve = (RelativeLayout)view. findViewById(R.id.el_celve);
        el_celve.setOnClickListener(this);
        el_training_camp = (RelativeLayout) view.findViewById(R.id.el_training_camp);
        el_training_camp.setOnClickListener(this);
        el_collect = (RelativeLayout) view.findViewById(R.id.el_collect);
        el_collect.setOnClickListener(this);
        el_discount = (RelativeLayout)view. findViewById(R.id.el_discount);
        el_discount.setOnClickListener(this);
        ll_calendar = (RelativeLayout)view. findViewById(R.id.ll_calendar);
        ll_calendar.setOnClickListener(this);
        el_about_us = (RelativeLayout) view.findViewById(R.id.el_about_us);
        el_about_us.setOnClickListener(this);
        cb_night_mode= (CheckBox)view. findViewById(R.id.cb_night_mode);
        iv_user_icon= (ImageView) view.findViewById(R.id.iv_user_icon);
        iv_user_icon.setOnClickListener(this);
        btn_login= (Button) view.findViewById(R.id.btn_login);
        btn_login.setOnClickListener(this);
        btn_registered= (Button) view.findViewById(R.id.btn_registered);
        btn_registered.setOnClickListener(this);
        return view;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.btn_login:
            startActivity(new Intent(getContext(), LoginActivity.class));
                break;
            case R.id.btn_registered:
                SMSVerification();
                break;

            case R.id.el_hearing_enhancement:
                Toast.makeText(getContext(), "即将实现该功能，敬请期待", Toast.LENGTH_SHORT).show();
                break;
            case R.id.el_message:
                Intent intent = new Intent(getActivity(), HTMLActivity.class);
                Bundle bundle=new Bundle();
                String html="https://weibo.com/5610812869/profile?topnav=1&wvr=6&is_all=1";
                bundle.putString("html",html);
                intent.putExtras(bundle);

                PendingIntent pi = PendingIntent.getActivity(getActivity(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

                Notification notification = new NotificationCompat.Builder(getActivity())
                        .setContentTitle("记单词效率能在王氏睡眠法下提高")//设置标题
                        .setContentText("日常吹牛：有关专家验证，王焕龙创建的王氏睡眠法，能在睡醒后提高记忆力，能长时间记住单词、口语等。")//设置内容
                        .setSmallIcon(R.mipmap.ic_launcher)//设置要显示的两个图片 小图片可以设置资源文件，大图片为bitmap类型所以需要decodeResource
                        .setContentIntent(pi)
                        .build();
                long[] vir = {0,100,200,330,330,330};
                notification.vibrate= vir;
                notification.flags|= Notification.FLAG_AUTO_CANCEL;

                NotificationManager manager = (NotificationManager) getActivity().getSystemService(NOTIFICATION_SERVICE);
                manager.notify(0, notification);

                break;
            case R.id.el_celve:
                Toast.makeText(getContext(), "该功能还未开发，敬请期待", Toast.LENGTH_SHORT).show();
                break;
            case R.id.el_training_camp:
                Toast.makeText(getContext(), "该功能还未开发，敬请期待", Toast.LENGTH_SHORT).show();
                break;
            case R.id.el_collect:
                Toast.makeText(getContext(), "该功能还未开发，敬请期待", Toast.LENGTH_SHORT).show();
                break;
            case R.id.el_discount:
                Toast.makeText(getContext(), "该功能还未开发，敬请期待", Toast.LENGTH_SHORT).show();
                break;
            case R.id.ll_calendar:
                Toast.makeText(getContext(), "即将实现该功能", Toast.LENGTH_SHORT).show();
                break;
            case R.id.el_about_us:
                startActivity(new Intent(getContext(), AboutUsActivity.class));
                break;



        }
    }

    public void SMSVerification() {

        SMSSDK.initSDK(getActivity(), "2357e0dc033d0", "d2de43b564a002f1a8f202ee83afac59");
        //创建注册页面
        RegisterPage registerPage = new RegisterPage();
        //显示界面
        registerPage.show(getActivity());

        //回调函数
        registerPage.setRegisterCallback(new EventHandler() {
            public void afterEvent(int event, int result, Object data) {
                // 解析注册结果
                if (result == SMSSDK.RESULT_COMPLETE) {
                    @SuppressWarnings("unchecked")
                    HashMap<String, Object> phoneMap = (HashMap<String, Object>) data;
                    String country = (String) phoneMap.get("country");
                    String phone = (String) phoneMap.get("phone");
                    Toast.makeText(getActivity(), "验证成功", Toast.LENGTH_SHORT).show();
                    // 提交用户信息（此方法可以不调用）
                    //registerUser(country, phone);
                }
            }
        });
    }


}
